package com.whfc.ms.api.base.controller;

import com.whfc.base.dto.MsgRecordDTO;
import com.whfc.base.dto.MsgStatisticsDTO;
import com.whfc.base.param.WarnMsgSearchParam;
import com.whfc.common.enums.AppWarnMsgChannel;
import com.whfc.common.result.*;
import com.whfc.common.util.AssertUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.entity.dto.msg.AppMsgNumDTO;
import com.whfc.entity.dto.msg.AppMsgToUserDTO;
import com.whfc.entity.dto.msg.AppWarnMsgDTO;
import com.whfc.entity.param.msg.AppMsgDelAllParam;
import com.whfc.entity.param.msg.AppMsgDelParam;
import com.whfc.entity.param.msg.AppMsgReadAllParam;
import com.whfc.entity.param.msg.AppMsgReadParam;
import com.whfc.fuum.entity.SysUser;
import com.whfc.ms.api.base.service.AppMessageService;
import com.whfc.ms.config.AbstractBaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * @author sun_guodong
 * @description 后台消息接口
 * @date 2019-12-05
 */
@RestController
@RequestMapping("/ms/api/msg")
public class AppMessageController extends AbstractBaseController {
    @Autowired
    private AppMessageService appMessageService;

    @GetMapping("/list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("pageNum") Integer pageNum,
                       @RequestParam("pageSize") Integer pageSize,
                       @RequestParam("deptId") Integer deptId,
                       @RequestParam(value = "state", required = false) Integer state,
                       @RequestParam(value = "moduleType", required = false) Integer moduleType) {
        logger.info("首页消息列表，loginer:{},pageNum:{},PageSize:{},deptId:{},state:{},moduleType:{}",
                user.getUsername(), pageNum, pageSize, deptId, state, moduleType);
        PageData<AppWarnMsgDTO> pageData = appMessageService.list(user, pageNum, pageSize, deptId, state, moduleType);
        return ResultUtil.success(pageData);
    }


    @GetMapping("/getUnreadMsgNum")
    public Result getUnreadMsgNum(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                  @RequestParam("deptId") Integer deptId) {
        logger.info("获取未读消息数,loginer:{},deptId:{}", user.getUsername(), deptId);
        AppMsgNumDTO appMsgNumDTO = appMessageService.getUnreadMsgNum(user, deptId);
        return ResultUtil.success(appMsgNumDTO);
    }

    @PostMapping("/read")
    public Result read(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestBody @Validated AppMsgReadParam request) {
        logger.info("将消息设置为已读，loginer:{},AppMsgReadParam:{}", user.getUsername(), request.toString());
        appMessageService.read(request);
        return ResultUtil.success();
    }

    /**
     * 获取可供选择的接收消息用户列表
     *
     * @param user
     * @param deptId
     * @return
     */
    @GetMapping("/getAvailableRecUsers")
    public Result getAvailableRecUsers(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                       @RequestParam("deptId") Integer deptId) {
        logger.info("获取可供选择的接收消息用户列表，loginer:{},deptId:{}", user.getUsername(), deptId);
        ListData<AppMsgToUserDTO> listData = appMessageService.getAvailableRecUsers(deptId);
        return ResultUtil.success(listData);
    }


    @PostMapping("/del")
    public Result del(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @Validated @RequestBody AppMsgDelParam request) {
        logger.info("删除消息,loginer:{},param:{}", user.getUsername(), request.toString());
        appMessageService.del(request);
        return ResultUtil.success();
    }

    @PostMapping("/delAll")
    public Result delAll(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                         @Validated @RequestBody AppMsgDelAllParam request) {
        logger.info("删除所有消息,loginer:{},param:{}", user.getUsername(), request.toString());
        appMessageService.delAll(user, request);
        return ResultUtil.success();
    }

    @PostMapping("/readAll")
    public Result readAll(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                          @Validated @RequestBody AppMsgReadAllParam request) {
        logger.info("标记读取所有消息,loginer:{},param:{}", user.getUsername(), request.toString());
        appMessageService.readAll(user, request);
        return ResultUtil.success();
    }

    /**
     * 获取待我处理的审批
     * @param user
     * @param
     * @return
     */
    @GetMapping("/getNewApproveWithMe")
    public Result getNewApproveWithMe(@SessionAttribute(SessionAttr.MS_USER) SysUser user,@RequestParam("deptId") Integer deptId) {
        logger.info("获取待我处理的审批,userId:{},username:{},deptId:{}", user.getId(),user.getUsername(),deptId);
        return ResultUtil.success(appMessageService.getNewApproveWithMe(user.getId(),deptId));
    }

    /**
     * 2.0.0报警消息类型数据统计
     *
     * @param deptId   项目ID
     * @param warnType 报警类型 1-报警消息 0-提醒消息
     * @return
     */
    @GetMapping("/warn/statistics/list")
    public R<List<MsgStatisticsDTO>> getWarnMsgStatistics(@RequestParam Integer deptId, @RequestParam Integer warnType) {

        logger.info("报警消息列表,sysUserId:{},deptId:{},warnType:{}", getLoginUser().getId(), deptId, warnType);
        if (warnType < 0 || warnType > 1) {
            return R.success(Collections.emptyList());
        }
        List<MsgStatisticsDTO> res = appMessageService.getWarnMsgStatistics(deptId, warnType, getLoginUser().getId());
        return R.success(res);
    }

    /**
     * 2.0.0报警消息分页
     *
     * @param param
     * @return
     */
    @GetMapping("/warn/page")
    public R<PageVO<MsgRecordDTO>> getWarnMsgPage(@Validated WarnMsgSearchParam param) {

        param.setUserId(getLoginUser().getId());
        param.setMsgChannel(AppWarnMsgChannel.WXMP.value());

        logger.info("报警消息明细分页:{}", param);

        AssertUtil.isTrue(param.getDeptId() == null || param.getModuleType() == null, "参数缺失");

        PageVO<MsgRecordDTO> res = appMessageService.getWarnMsgPage(param);
        return R.success(res);
    }
}
